﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data_Access_Layer;
using Raporty;

namespace Moje_Finanse
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            groupBox.Hide();
        }
        
        #region Zdarzenia GUI

        private void dodajTransakcje_Click(object sender, EventArgs e)
        {
            DodajWydatek okno = new DodajWydatek(this, monthCalendar.SelectionStart);
            okno.ShowDialog();
        }

        private void btnFinanse_Click(object sender, EventArgs e)
        {
            groupBox.Show();
            refreshGridView(monthCalendar.SelectionStart);
        }

        /// <summary>
        /// Zdarzenie, które za każdym razem zmiany daty ustawia pare elementów w oknie
        /// oraz styluje DataGridView
        /// </summary>
        private void monthCalendar_DateChanged(object sender, DateRangeEventArgs e)
        {
            // Bindujemy zgodnie  z wybraną datą (e - argument przekazany w zdarzeniu)
            dataGridView.DataSource = DbManager.Instance.GetTransactionsByDate(e.Start);
            sumDaily();
        }

        /// <summary>
        /// Zdarzenie zaznacza cały wiersz, aby zdobyć Id
        /// </summary>
        private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            dataGridView.ClearSelection();
            if (e.Button == MouseButtons.Right)
            {
                int rowSelected = e.RowIndex;
                if (e.RowIndex != -1)
                {
                    this.dataGridView.Rows[rowSelected].Selected = true;
                }
            }
        }

        private void toolStripMenuDelete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Czy na pewno chcesz usunąć?", "Potwierdzenie", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                System.Guid id = (System.Guid)dataGridView.SelectedRows[0].Cells[0].Value;
                DbManager.Instance.DeleteTransactionById(id);
            }
            refreshGridView(monthCalendar.SelectionStart);
        }

        private void btnKategorie_Click(object sender, EventArgs e)
        {
            new DodajKategorie().ShowDialog();
        }

        private void btnRaporty_Click(object sender, EventArgs e)
        {
            new Raporty.Raporty().ShowDialog();
        }

        #endregion

        /// <summary>
        /// Odświeża grida, poprzez przymusowe wywołanie zmiany daty (i DataSource)
        /// </summary>
        public void refreshGridView(DateTime date)
        {
            this.monthCalendar_DateChanged(this, new DateRangeEventArgs(date, DateTime.Today) { });
        }

        private void styleGridView(DataGridView dataGrid)
        {
            // Stylujemy grida
            dataGrid.ClearSelection();
            dataGrid.Columns[0].Visible = false;
            dataGrid.Columns[1].HeaderText = "Opis";
            dataGrid.Columns[2].HeaderText = "Wartość";
            dataGrid.Columns[2].DefaultCellStyle.Format = "c";
            dataGrid.Columns[3].HeaderText = "Kategoria";
            foreach (DataGridViewRow row in dataGrid.Rows)
            {
                decimal value = (decimal)row.Cells[2].Value;
                row.DefaultCellStyle.ForeColor = value >= 0 ? Color.DarkGreen : Color.DarkRed;
            }
        }

        /// <summary>
        /// Liczy bilans dnia
        /// </summary>
        private void sumDaily()
        {
            decimal balance = DbManager.Instance.GetBalanceToDate(monthCalendar.SelectionStart);
            suma.ForeColor = balance >= 0 ? Color.Green : Color.Red;
            suma.Text = string.Format("{0:C}", balance);
        }

        private void dataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            styleGridView(dataGridView);
        }
    }
}
